<template>
  <div class="order-checked-main">
    <div class="header-main">
      <span @click="goBack"><van-icon name="arrow-left" /></span>
      <div class="center">{{ $t('h.ORDER_CONFIRMATION')}}</div>
    </div>
    <div class="order-main">
      <div  class="chose-address" v-if="defaultArea" @click="addressListShow = true">
        <span>
           <van-icon size="24" name="location-o" />
            <span>{{defaultArea?.shortname}}-{{defaultArea?.province}}-{{defaultArea?.city}}-{{defaultArea?.address}}</span>
        </span>
        <van-icon  name="arrow" />
      </div>
      <div v-if="false" class="get-address">
        <div class="left">
          <van-icon style="margin-top: 15px" size="28" name="location-o" />
          <div class="left-address">
            <p>lishang 1|2313213</p>
            <p>13012341234</p>
          </div>
        </div>
        <van-icon  name="arrow" />
      </div>
      <!-- 商品 BEGIN    -->
      <div class="card-box">
        <template v-for="(shop,index) in orderGoods">
          <div class="header-box">
            <van-checkbox icon-size="16px" v-model="shop.checked" checked-color="#f89900" @click="handleShopCheckboxClick(index)" >{{shop.shopName}}</van-checkbox>
          </div>
          <van-checkbox-group v-model="shop.checkes" @change="handleGroupChange($event,index)" :key="`shop${index}`" ref="shopGroup">
            <template v-for="(item ,idx) in shop.goods">
              <div class="card-box-item" v-if="item.num">
                <div class="left-box">
                  <van-checkbox :name="item" icon-size="16px" checked-color="#f89900" :key="`shop${index}#goods${idx}`" :ref="`shop${index}GoodsCheck`"></van-checkbox>
                </div>
                <div class="right-box">
                  <img :src="item.mainimage" alt="">
                  <div class="right">
                    <p class="title">{{item.name}} </p>
                    <p class="sku_box">
                      <!--                        {{ $t('h.A064') }}:黑色和红色-->
                    </p>
                    <p class="price">
                      <span>${{ item.price }}</span>
                      <van-stepper button-size="22px" :key="`goodsNumStepper${index}`" :min="0" v-model="item.num" @change="handleStepperChange($event,index,idx)"/>
                    </p>
                  </div>
                </div>
              </div>
            </template>
          </van-checkbox-group>

        </template>
      </div>
      <!--   商品 END  -->
      <div class="product-info">
        <van-cell-group :border="false">
          <van-cell>
            <template slot="title">
              <span class="label">{{ $t('h.ITEM_AMOUNT') }}</span>
            </template>
            <template #right-icon>
              <span class="right" style="color: #aaa">${{totalPrice}}</span>
            </template>
          </van-cell>
          <van-cell>
            <template slot="title">
              <span class="label">{{ $t('h.SHIPPING_COST') }}</span>
            </template>
            <template #right-icon>
              <span class="right" style="color: #002FFF">$0</span>
            </template>
          </van-cell>
          <van-cell>
            <template slot="title">
              <span class="label">{{ $t('h.TAXES') }}</span>
            </template>
            <template #right-icon>
              <span class="right" style="color: #002FFF">$0</span>
            </template>
          </van-cell>
          <van-cell>
            <template slot="title">
              <span class="label">{{ $t('h.TOTAL') }}</span>
            </template>
            <template #right-icon>
              <span class="right" style="color: #002FFF">${{totalPrice}}</span>
            </template>
          </van-cell>

        </van-cell-group>
      </div>
    </div>
    <div class="safe"></div>
    <div class="submit-bottom">
      <span>{{ $t('h.TOTAL') }}：<b>${{totalPrice}}</b></span>
      <span>{{ $t('h.SELECTED') }}：<b>{{checkedNums}}</b>{{ $t('h.ITEM_UNIT_PRICE') }}</span>
      <button @click="submit">{{ $t('h.CONFIRM') }}</button>
    </div>
    <van-popup v-model="addressListShow" position="bottom" round :style="{ height: '40%' }" >
      <van-picker
          show-toolbar
          :cancel-button-text="$t('h.CANCEL')"
          :confirm-button-text="$t('h.CONFIRM')"
          :columns="addressListData"
          @cancel="addressListShow = false"
          @confirm="addressConfirm"
      />
    </van-popup>
  </div>
</template>

<script>
import {mapGetters} from 'vuex';
import {Toast} from 'vant';
import {getAddress, getDefaultArea} from '@/api/apiFox';

export default {
  name: "index",
  data(){
    return{
      addressListShow:false,
      defaultArea:null,
      checked:true,
      value:1,
      totalPrice:0,
      checkedNums:0,
      orderGoods:[]
    }
  },
  computed: {
   ...mapGetters('order',['order']),
  },
  mounted() {
    this.getListData();
    getDefaultArea().then(res => {
      this.defaultArea = res.data
    })
    this.orderGoods = JSON.parse(JSON.stringify(this.order));
    if(this.$route.query?.auto){
      this.$nextTick(() => {
        this.$refs['shopGroup'].forEach(item => {
          item.toggleAll(true);
        })
      })
    }
  },
  methods:{
    addressConfirm({value}){
      this.addressListShow = false;
      this.defaultArea = value
    },
    getListData(){
      getAddress().then(res => {
        this.addressListData = res.data.map(item => {
          return {
            text: `${item?.shortname}-${item?.province}-${item?.city}-${item?.address}`,
            value:item
          }
        })
      })
    },
    submit(){
      if(!this.checkedNums){
        Toast.fail(this.$t('h.请选择商品'));
      }else{
        this.$router.replace({
          path:"/pay",
          query:{
            price:this.totalPrice
          }
        })
      }
    },
    calc(){
      const shops = this.orderGoods.filter(item => item.checked);
      let num = 0;
      let price = 0;
      shops.forEach(shop => {
        shop.checkes.forEach(g => {
          num += g.num;
          price += g.num * g.price;
        })
      })
      this.totalPrice = price.toFixed(2);
      this.checkedNums = num;
    },
    handleShopCheckboxClick(shopIndex){
      this.$refs['shopGroup'][shopIndex].toggleAll(this.orderGoods[shopIndex].checked);
      this.calc();
    },
    handleStepperChange(val,shopIndex,goodsIndex){
      this.calc();
    },
    handleGroupChange(values,shopIndex){
      this.$set(this.orderGoods[shopIndex],'checked',!!values.length)
      this.calc();
    },
    goBack(){
      this.$router.go(-1)
    },
  }
}
</script>

<style scoped lang="scss">
.card-box{
  width: 100%;
  overflow-y: scroll;
  padding: 3%;
  box-sizing: border-box;
  .header-box{
    width: 100%;
    height: 30px;
    font-size: 14px;
    padding: 0 0 0 20px;
    box-sizing: border-box;
  }
  .card-box-item{
    width: 100%;
    background: #fff;
    margin-bottom: 20px;
    display: flex;
    padding: 3% 0;
    .left-box{
      width: 40px;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .right-box{
      width: 100%;
      height: 100px;
      display: flex;
      align-items: center;
      img{
        width: 60px;
        height: 60px;
      }
      .right{
        width: 100%;
        height: 100px;
        padding: 3%;
        box-sizing: border-box;
        .title{
          width: 100%;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -moz-box-orient: vertical;
          -webkit-line-clamp: 2;
          font-size: 14px;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .sku_box{
          height: 20px;
          margin-bottom: 10px;
          font-size: 12px;
          width: 100%;
        }
        .price{
          width: 100%;
          height: 30px;
          display: flex;
          justify-content: space-between;
          span{
            color: #f89220;
            font-size: 14px;
          }
        }
      }
    }
  }
  .safe{
    height: 110px;
    width: 100%;
  }
  .submit-bottom{
    height: 60px;
    background: #fff;
    position: fixed;
    bottom: 65px;
    left: 0;
    width: 100%;
    font-size: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 3%;
    box-sizing: border-box;
    span{
      font-size: 16px;
      b{
        color: #002FFF;
      }
    }
    .info-box{
      p:nth-child(1){
        font-size: 14px;
        b{
          color: #f89220;
          font-size: 16px;
        }
      }
      p:nth-child(2){
        font-size: 14px;
        b{
          color: #f89220;
          font-size: 16px;
          margin: 0 5px;
        }
      }
    }
    button{
      width: 100px;
      height: 40px;
      border: none;
      background: #f89220;
      color: #fff;
      font-size: 14px;
      border-radius: 5px;
    }
  }
}
.order-checked-main{
  width: 100%;
  background: #f6f6f6;
  min-height: 100vh;

  .header-main{
    width: 100%;
    height: 45px;
    padding: 15px 0 0 0;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    background: #fff;
    span{
      position: absolute;
      left: 20px;
      font-size: 18px;
    }
    .center{
      width: 100%;
      height: 45px;
      text-align: center;
      line-height: 45px;
      font-size: 15px;
    }
  }
  .order-main{
    width: 100%;
    padding: 3%;
    box-sizing: border-box;
    .chose-address{
      width: 100%;
      height: 45px;
      background: url('../../assets/image/bgk.png') no-repeat 50%;
      background-size: cover;
      font-size: 16px;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 3%;
      box-sizing: border-box;
    }
    .get-address{
      width: 100%;
      height: 80px;
      background: #fff;
      display: flex;
      padding: 3%;
      box-sizing: border-box;
      align-items: center;
      .left{
        width: 100%;
        height: 80px;
        font-size: 18px;
        display: flex;
        padding: 3%;
        box-sizing: border-box;
        .left-address{
          width: 100%;
          margin-left: 20px;
          padding: 10px 0 0 0;
          box-sizing: border-box;
          p:nth-child(1){
            font-size: 13px;
          }
          p:nth-child(2){
            color: #aaa;
            font-size: 13px;
            margin-top: 10px;
          }
        }
      }
    }
    .chose{
      margin-top: 20px;
      width: 100%;
      height: 20px;
      font-size: 14px;
      padding: 0 3%;
      box-sizing: border-box;
    }
    .product{
      width: 100%;
      height: 100px;
      background: #fff;
      margin-top: 20px;
      display: flex;
      .left{
        width: 35px;
        height: 100px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .right{
        width: 100%;
        height: 100px;
        align-items: center;
        display: flex;
        img{
          height: 60px;
          width: 60px;
          margin: 0 15px;
        }
        .info{
          width: 100%;
          height: 100px;
          padding: 3%;
          box-sizing: border-box;
          p:nth-child(1){
           font-size: 14px;
            color: #000;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -moz-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          p:nth-child(2){
            height: 20px;
            margin-top: 15px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .money{
              font-size: 15px;
              color: #002FFF;
            }
          }
        }
      }
    }
    .product-info{
      width: 100%;
      min-height: 100px;
      border-radius: 5px;
      background: #fff;
      padding: 3%;
      box-sizing: border-box;
      .label{
        font-size: 12px;
      }
      .right{
        font-size: 12px;
      }
    }
  }
  .safe{
    width: 100%;
    height: 60px;
  }
  .submit-bottom{
    height: 60px;
    background: #fff;
    position: fixed;
    bottom: 0;
    width: 100%;
    font-size: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 3%;
    box-sizing: border-box;
    span{
      font-size: 16px;
      b{
        color: #002FFF;
      }
    }
    button{
      width: 100px;
      height: 35px;
      border: none;
      background: #002FFF;
      color: #fff;
      font-size: 14px;
      border-radius: 5px;
    }
  }
}
</style>
